<?php
include 'include/functions.php';
//set session save path
	session_save_path($session_path);
	// Initialize a session. 
	session_start();
	$count++;
	$sessionId = session_id();
	
	if(!$userID)
	{
		header("Location: logoff.php"); /* Redirect browser */
		/* Make sure that code below does not get executed when we redirect. */
		exit;
	}
?>
<!DOCTYPE html PUBLIC "-//W3C//Dtd html 4.01 Transitional//EN"
"http://www.w3.org/tr/html4/loose.dtd">
<html><!-- InstanceBegin template="/Templates/MAIN.dwt.php" codeOutsideHTMLIsLocked="false" -->
  <head>
    <!-- InstanceBeginEditable name="doctitle" -->
    <title>JoshHughes.com NFL Football Pool</title>
    <!-- InstanceEndEditable -->
	<link rel="stylesheet" href="css/football.css" type="text/css">
    <script src="scripts/prototype.js" type="text/javascript"></script>
	<script src="scripts/scriptaculous.js" type="text/javascript"></script>
	<script type="text/javascript">
	// Dynamic Iframe loader
	function loadIframe(theFrame, theURL) {
		document.getElementById(theFrame).src=theURL;
	}
	
	// resizes Iframe according to content
	function resizeMe(obj){ 
	docHeight = mainContent.document.body.scrollHeight
	 obj.style.height = docHeight + 'px'
	 } 
	</script>
    <!-- InstanceBeginEditable name="head" --><!-- InstanceEndEditable -->
  </head>
<body>
<div id="pagewidth" >
	<div id="header" >
	
		<?PHP include_once("layout/header.html"); ?>
		
	</div>
			<div id="wrapper" class="clearfix" > 
			<div id="maincol" >

			<!-- InstanceBeginEditable name="Contents" -->

				<?php
				
					//connect to Database
					dbConnect();		
					
					if($update != "")
					{
						switch ($update)
						{						
							   case "password" :
								 ?>
							<link rel="stylesheet" href="css/NFLPool.css" type="text/css">
							<table width="500" cellpadding="5" cellspacing="0">
							<tr>
							<td class="redbg" align="Left"><span class="whitelink">Change Your Password</span></td>
							  </tr>
								<td class="redbg" align="Left">
								<form name="registerForm" action="<?PHP echo $PHP_SELF; ?>" method="POST">
								   <table width="500" border="0" cellspacing="3" cellpadding="3" border="0" bgcolor="#FFFFFF">
									<tr> 
												<td height="25">
										<B>Old Password:</B>
										</td>
												<td height="25">
										<input type="password" name="oldpass">
										</td>
									</tr>
									<tr> 
												<td height="25">
										<B>New Password:</B>
										</td>
												<td height="25">
										<input type="password" name="newpass">
										</td>
									</tr>
									<tr> 
												<td height="25">
										<B>Confirm New Password:</B>
										</td>
												<td height="25">
										<input type="password" name="newpass2">
										</td>
									</tr>
									<tr> 
												<td height="25" colspan="2"><div align="center"> 
										<input type="submit" name="Submit" value="Change Password">
										<input type="reset" name="Submit2" value="Reset Form"></div>
										</td>
									</tr>
									</table>
								 </form>
								</td>
							  </tr>
							</table>
								 <?
							   break;
							
							 
							   case "emailprefs" :	//Update user Email Preferences
								//Get an Array of distinct users.
								$SQL = "SELECT ShowEmail, RemindMe, TuesdayEmail, EmailPicks FROM NFLUsers WHERE ID = $userID";
								$IDresult = mysql_query($SQL) or die("Cannot query the database.<br>" . mysql_error());
								$IDrow = mysql_fetch_array($IDresult);
								 ?>
							<link rel="stylesheet" href="css/NFLPool.css" type="text/css">
							<table width="500" cellpadding="5" cellspacing="0">
							<tr>
							<td class="bluebg" align="Left"><span class="whitelink">Change Email Preferences</span></td>
							  </tr>
								<td class="bluebg" align="Left">
								<form name="registerForm" action="<?PHP echo $PHP_SELF; ?>" method="POST">
								   <table width="500" border="0" cellspacing="0" cellpadding="0" border="0" bgcolor="#FFFFFF">
									<tr>
										<td width="5%">&nbsp;</td>
												<td height="25">
										<B>Keep Email Address Private:</B>
										</td>
												<td height="25">
										<input name="ShowEmail" type="Checkbox" id="ShowEmail" value="yes" <?
										if($IDrow["ShowEmail"] == 0) { echo "checked"; }
										?>>
										</td>
										<td width="5%">&nbsp;</td>
									</tr>
									<tr bgcolor="#EEEEEE">
										<td width="5%">&nbsp;</td>
										<td height="25">
										<B>Remind me when I need to make picks 
										<select name="RemindMe" id="RemindMe">
										<?
											for ($i = 0; $i < 7; $i++)
											{
												if ($i == $IDrow["RemindMe"])
													echo "<option value=\"$i\" selected>$i</option>";
												else
													echo "<option value=\"$i\">$i</option>";
											}
										?>
										</select> days before games.										
										</B>
										</td>
										<td height="25">
										&nbsp;
										</td>
										<td width="5%">&nbsp;</td>
									</tr>
									<tr>
										<td width="5%">&nbsp;</td>
												<td height="25">
										<B>Send me weekly score updates on Tuesdays</B>
										</td>
												<td height="25">
										<input name="TuesdayEmail" type="Checkbox" id="TuesdayEmail" value="yes" <?
										if($IDrow["TuesdayEmail"] == 1) { echo "checked"; }
										?>>
										</td>
										<td width="5%">&nbsp;</td>
									</tr>
									<tr bgcolor="#EEEEEE">
										<td width="5%">&nbsp;</td>
												<td height="25">
										<B>Send me user's picks shortly after games begin.</B>
										</td>
												<td height="25">
										<input name="EmailPicks" type="Checkbox" id="EmailPicks" value="yes" <?
										if($IDrow["EmailPicks"] == 1) { echo "checked"; }
										?>>
										</td>
										<td width="5%">&nbsp;</td>
									</tr>
									<tr> 
										<td width="5%">&nbsp;</td>
												<td height="25" colspan="2"><div align="center"> 
										<input type="submit" name="Submit" value="Change Preferences">
										<input type="reset" name="Submit2" value="Reset Form"></div>
										</td>
										<td width="5%">&nbsp;</td>
									</tr>
									</table>
								 </form>
								</td>
							  </tr>
							</table>
								 <?
							   break;
							
						}//end switch ($update)
					}//end if($update)
					
					
					if($Submit != "")
					{
						switch ($Submit) {
					
						case "Change Password" :
							//Get an Array of distinct users.
							$SQL = "SELECT Password FROM NFLUsers WHERE ID = $userID";
							$IDresult = mysql_query($SQL) or die("Cannot query the database.<br>" . mysql_error());
					
							if($IDrow = mysql_fetch_array($IDresult)){
								if (($oldpass != $IDrow["Password"]) || ($newpass != $newpass2)) {
									echo "There seems to be a problem with the information you entered, please <a href=\"".$PHP_SELF."?update=password\">Go Back</a> and try again.";
								} else {
									mysql_query("UPDATE NFLUsers SET Password = '$newpass' WHERE ID = $userID") or die("Cannot query the database.<br>" . mysql_error());
									echo "<H2>Your password was successfully changed!</H2>";
								}
							}//end if($IDrow = mysql_fetch_array($IDresult))
						break;
					
						case "Change Preferences":
							if($ShowEmail == "yes") {
								$ShowEmail = 0;
							} else {
								$ShowEmail = 1;
							}//end if($ShowEmail == "yes")
							if($TuesdayEmail == "yes") {
								$TuesdayEmail = 1;
							} else {
								$TuesdayEmail = 0;
							}//end if($TuesdayEmail == "yes")
							if($EmailPicks == "yes") {
								$EmailPicks = 1;
							} else {
								$EmailPicks = 0;
							}//end if($EmailPicks == "yes")
							//Open Record Set using SQL Query
							mysql_query("UPDATE NFLUsers SET ShowEmail = '$ShowEmail', RemindMe = '$RemindMe', TuesdayEmail = '$TuesdayEmail', EmailPicks = '$EmailPicks' WHERE ID = $userID") or die("Cannot query the database.<br>" . mysql_error());
							if($ShowEmail == 0) {
								$ShowEmail = "YES";
							} else {
								$ShowEmail = "NO";
							}//end if($ShowEmail == 0)
							if($TuesdayEmail == 1) {
								$TuesdayEmail = "YES";
							} else {
								$TuesdayEmail = "NO";
							}//end if($TuesdayEmail == 1)
							if($EmailPicks == 1) {
								$EmailPicks = "YES";
							} else {
								$EmailPicks = "NO";
							}//end if($EmailPicks == 1)
							echo "<H2>Your email preferences have been changed!</H2>
							You said <I style=\"color: #FF0000; font-weight: bold;\">$ShowEmail</I> to &quot;Keep My Email Private&quot;,<BR>
							<I style=\"color: #FF0000; font-weight: bold;\">$EmailPicks</I> to &quot;Send me user's picks shortly after games begin.&quot;,<BR>
							<I style=\"color: #FF0000; font-weight: bold;\">$TuesdayEmail</I> to &quot;Send me weekly updates on Tuesdays&quot;, and 
							
							Remind me to make picks <I style=\"color: #FF0000; font-weight: bold;\">$RemindMe</I> days before a game.";
						break;
					
						}//end switch ($Submit)
					
					}//end if($Submit)
				?>

			<!-- InstanceEndEditable -->
			
			</div>
			<div id="leftcol" >
				<script type="text/javascript">
				loadIframe('monkeyframe','..randomrants.php');
				loadIframe('pollframe','../polls/vote.php');
				</script>
				<div class="redbg">
					<span class="whitelink" style="float:left;">Log On</span><span class="permLink" style="float:right;"><a href="register.php">Register</a></span>
					<iframe id="loginframe" src="menu.php" height="190" width="175" frameborder="0" scrolling="no" ></iframe>
				</div>
				
				<div class="bluebg">
					<table width="100%" cellpadding="0" cellspacing="0" border="0">
						<tr>
							<td align="left"><a href="standings.php" class="whitelink">Standings</a> <span class="whitelink"> Top 5</span></td>
							<td align="right"><img src="images/showhide.gif" title="Show/Hide Standings" alt="Show/Hide Standings" border="0"  onclick="new Effect.toggle('StandingsDiv', 'blind');" /></td>
						</tr>
					</table>
					<div id="StandingsDiv" style="background-color:#FFFFFF;">
					<table width="100%" cellpadding="0" cellspacing="0" border="0" >
					<tr><th width="5%">&nbsp;</th><th width="60%" align="left">UserName</th><th width="30%" align="left">Score</th><th width="5%">&nbsp;</th>
					<?
					dbConnect();
					//Write SQL Query to extract specified data
					$SQL = "SELECT ID, username, Score, flag ";
					if($CurrWeek > 17)
					{
						$SQL .= "PlayOffs FROM NFLUsers WHERE PlayOffs=1 AND ";
					}
					else
					{
						 $SQL .= "FROM NFLUsers WHERE ";
					}
					$SQL .= "flag=1 ORDER BY Score DESC, username ASC LIMIT 5";
					//Open Record Set using SQL Query
					$result = mysql_query($SQL) or die("Cannot query the database.<br>" . mysql_error());
					do{
						if (($myrow["username"] != Null) || ($myrow["username"] != ""))
						{
							echo "<tr><td>&nbsp;</td><td valign=\"center\"><B><a href=\"members.php?ID=".$myrow["ID"]."\" class=\"permLink\">".$myrow["username"]."</a></B></td><td align=\"center\">".$myrow["Score"]."</td><td>&nbsp;</td></tr>";
						}
					}while($myrow = mysql_fetch_array($result));
					
					?>
					<tr><td colspan="4" align="center"><br/><B><a href="standings.php" class="permLink"> More 2007 Standings ... </a></B></td></tr>
					<tr><td colspan="4"><hr class="redbg"/></td></tr>
					<tr><td>&nbsp;</td><td colspan="2" align="left"><B>History</B></td><td>&nbsp;</td></tr>
					<tr><td>&nbsp;</td><td colspan="2" align="right"><B><a href="history/2006" class="permLink">2006 Standings</a></B></td><td>&nbsp;</td></tr>
					<tr><td>&nbsp;</td><td colspan="2" align="right"><B><a href="history/2005" class="permLink">2005 Standings</a></B></td><td>&nbsp;</td></tr>
					</table>
					</div>
				</div>
				
				<div class="redbg">
					<table width="100%" cellpadding="0" cellspacing="0" border="0">
						<tr>
							<td align="left"><span class="whitelink">Need Help?</span></td>
							<td align="right"><img src="images/showhide.gif" title="Show/Hide Random Monkey's Pick" alt="Show/Hide Random Monkey's Pick" border="0"  onclick="new Effect.toggle('monkeyframe', 'blind');" /></td>
						</tr>
					</table>
					<iframe style="display:none;" id="monkeyframe" name="monkeyframe" src="randomrants.php" width="175" frameborder="0" scrolling="no" ></iframe>
				</div>
				<div class="bluebg">
					<table width="100%" cellpadding="0" cellspacing="0" border="0">
						<tr>
							<td align="left"><span class="whitelink">Poll</span></td>
							<td align="right"><img src="images/showhide.gif" title="Show/Hide Pool Box" alt="Show/Hide Pool Box" border="0"  onclick="new Effect.toggle('pollframe', 'blind');" /></td>
						</tr>
					</table>
					<iframe style="display:none;" id="pollframe" name="pollframe" src="polls/vote.php"  width="175" frameborder="0" scrolling="no" ></iframe>
				</div>
				
				<div class="redbg">
					<table width="100%" cellpadding="0" cellspacing="0" border="0">
						<tr>
							<td align="left"><span class="whitelink">Suggestion Box</span></td>
							<td align="right"><img src="images/showhide.gif" title="Show/Hide Suggestion Box" alt="Show/Hide Suggestion Box" border="0"  onclick="new Effect.toggle('suggestframe', 'blind');" /></td>
						</tr>
					</table>
					<iframe style="display:none;" id="suggestframe" name="suggestionsframe" src="suggestions.php" height="250" width="175" frameborder="0" scrolling="no" ></iframe>
				</div>
			</div>
</div>
	<div id="footer" >
		<span class="permLink" style="margin-left:5px ">
			<a href="/">Football Pool Home</a> <span class="pipe">|</span>
			<a href="http://www.joshhughes.com">JoshHughes.com Home</a> <span class="pipe">|</span>
			<a href="teams.php">Team Info</a> <span class="pipe">|</span>
			<a href="javascript:window.external.AddFavorite('http://football.joshhughes.com', 'Football Pool @ JoshHughes.com')" target="_self">Bookmark</a> <span class="pipe">|</span>
			<a href="rules.php">Rules &amp; FAQ</a></span>
	</div>
</div>
</body>
<!-- InstanceEnd --></html>